# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Zion Nimchuk <zionnimchuk@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com

_realname=rust
_bootstrapping=yes
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-rust-docs")
pkgver=1.56.1
pkgrel=1
pkgdesc="Systems programming language focused on safety, speed and concurrency (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.rust-lang.org/"
license=('MIT' 'Apache')
makedepends=("bison"
             "flex"
             "git"
             "${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-libssh2"
             "${MINGW_PACKAGE_PREFIX}-lldb"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-python"
             $([[ "$_bootstrapping" == "no" ]] && echo "${MINGW_PACKAGE_PREFIX}-rust")
             "${MINGW_PACKAGE_PREFIX}-zlib")
# Bug in strip breaks the DLLs: https://github.com/msys2/MINGW-packages/issues/9655
options=('staticlibs' '!strip')
#install=rust.install
noextract=(${_realname}c-${pkgver}-src.tar.gz)
source=("https://static.rust-lang.org/dist/${_realname}c-${pkgver}-src.tar.gz"{,.asc}
        "0001-add-missing-libs.patch"
        "0004-unbundle-gcc.patch"
        "0005-win32-config.patch"
        "0007-clang-subsystem.patch"
        "0008-disable-self-contained.patch")
sha256sums=('c3898dfaadaa193dc88ddbc5345946a163211b58621df1cfff70186b4fc79511'
            'SKIP'
            'c706841ace2ef76fd1004b9c0486ea9ef7d99073c5ce2884e4c97fa4a7beab25'
            '6cc3234644768e24824e455d6304585c717ae25aa53ebf9cf1f1dea87bf869b7'
            'c4e5ffeef84296d39c3e3e8f807fc8b33ce786b1e4edb21eef26b053586aca27'
            '9e2e2eb9c0684f329b1ce6598fcb66727abf84121087b24670a9cfc44a514888'
            '29f84cb8e05ce304e102e28912a3b4464add406a8ec37a6c6d717b9b7d81b67b')
validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE'  # Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard <tstellar@redhat.com>
              'B6C8F98282B944E3B0D5C2530FC3042E345AD05D') # Hans Wennborg <hans@chromium.org>

prepare() {
  [[ -d ${srcdir}/${_realname}c-${pkgver}-src ]] && rm -rf ${srcdir}/${_realname}c-${pkgver}-src
  tar -xzf ${srcdir}/${_realname}c-${pkgver}-src.tar.gz -C ${srcdir} || true

  cd ${srcdir}/${_realname}c-${pkgver}-src
  patch -p1 -i "${srcdir}/0001-add-missing-libs.patch"
  patch -p1 -i "${srcdir}/0004-unbundle-gcc.patch"
  patch -p1 -i "${srcdir}/0005-win32-config.patch"
  patch -p1 -i "${srcdir}/0008-disable-self-contained.patch"

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    patch -p1 -i "${srcdir}/0007-clang-subsystem.patch"
  fi
}

build() {
  [[ -d "${srcdir}/${MSYSTEM}" ]] && rm -rf "${srcdir}/${MSYSTEM}"
  mkdir -p "${srcdir}/${MSYSTEM}" && cd "${srcdir}/${MSYSTEM}"

  # The ultimate hack to let the bootstrap compiler use libgcc* libs
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    export GCC_LIBS_HACK="$(cygpath -am build/missing-libs-hack)"
    mkdir -p "${GCC_LIBS_HACK}"
    cp "$(cygpath -u $(clang -print-libgcc-file-name))" "${GCC_LIBS_HACK}/libgcc.a"
    cp "${MINGW_PREFIX}/lib/libunwind.a" "${GCC_LIBS_HACK}/libgcc_eh.a"
    cp "${MINGW_PREFIX}/lib/libunwind.dll.a" "${GCC_LIBS_HACK}/libgcc_s.a"
    export RUSTFLAGS_BOOTSTRAP="-C link-arg=-L$(cygpath -am build/missing-libs-hack)"
  fi

  #We have to do the following because rust doesn't count x86_64-w64-mingw32 as a target triple
  OSTYPE="$CARCH-pc-windows-gnu"

  local -a _rust_conf=()
  if [ "${_bootstrapping}" = "no" ]; then
    _rust_conf+=("--local-rust-root=$(cygpath -m ${MINGW_PREFIX})")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix;--sysconfdir;--localstatedir" \
  ../${_realname}c-${pkgver}-src/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var/lib \
    --build=$OSTYPE \
    --host=$OSTYPE \
    --target=$OSTYPE \
    --release-channel=stable \
    --enable-ninja \
    --enable-extended \
    --disable-codegen-tests \
    --llvm-root=${MINGW_PREFIX} \
    --python=${MINGW_PREFIX}/bin/python \
    ${_rust_conf}

  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py build --verbose --stage 2

  #create the install at a temporary directory
  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  DESTDIR="$PWD"/dest-rust ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py install --stage 2 --verbose

  # move docs out of the way for splitting
  mv dest-rust/${MINGW_PREFIX}/share/doc dest-doc

  rm -f dest-rust/${MINGW_PREFIX}/bin/libgcc*.dll
  rm -f dest-rust/${MINGW_PREFIX}/bin/libstd*.dll
  rm -f dest-rust/${MINGW_PREFIX}/bin/libwinpthread*.dll
  rm -f dest-rust/${MINGW_PREFIX}/lib/rustlib/$OSTYPE/lib/self-contained/*
}

check() {
  cd "${srcdir}/${MSYSTEM}"
  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py test --stage 2 --exclude src/test/debuginfo
}


package_rust() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-curl"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libssh2")

  cd "${srcdir}/${MSYSTEM}"

  cp -a dest-rust/* "$pkgdir"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"

  # delete unnecesary files, e.g. components and manifest files only used for the uninstall script
  cd "$pkgdir"/${MINGW_PREFIX}/lib/rustlib
  rm components install.log manifest-* rust-installer-version uninstall.sh

  install -d "$pkgdir/${MINGW_PREFIX}/share/bash-completion/completions"
  mv "${pkgdir}${MINGW_PREFIX}/etc/bash_completion.d/cargo" \
     "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/cargo"

}

package_rust-docs() {
  pkgdesc='Documentation for the Rust programming language'
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<1.52.0")

  cd "${srcdir}/${MSYSTEM}"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/LICENSE-APACHE"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/LICENSE-MIT"

  install -d "$pkgdir"/${MINGW_PREFIX}/share/
  cp -a dest-doc "$pkgdir"/${MINGW_PREFIX}/share/doc
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
